<!--<link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">-->
<link rel="stylesheet" href="lib/jq-module/zyupload/zyupload-1.0.0.min.css" media="all">
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote">
            若上传照片为毕业合照，标题请写为：河南大学**学院**届（**）级**系**专业**班毕业合影
        </blockquote>
        <form class="layui-form" action="">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>相册信息</legend>
            </fieldset>
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入相册标题"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">简介</label>
                <div class="layui-input-block">
                    <textarea name="describe" autocomplete="off" placeholder="请输入相册简介" class="layui-input"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否公开</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="isPublic" lay-skin="switch" lay-text="是|否">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">专业</label>
                <div class="layui-input-inline">
                    <select name="major" lay-verify=""  id="major-select-search-add" lay-filter="major-select-search-add">
                        <option value="">需要先选择学院</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">年级</label>
                <div class="layui-input-inline">
                    <select name="grade" lay-verify="" id="grade-select-search-add" lay-filter="grade-select-search-add">
                        <option value="">需要先选择专业</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">班级</label>
                <div class="layui-input-inline">
                    <select name="class" lay-verify="" id="class-select-search-add" lay-filter="class-select-search-add">
                        <option value="">需要先选择年级</option>
                    </select>
                </div>
            </div>
            <center>
                <button class="layui-btn number-of-people-btn" lay-submit="" lay-filter="save">保存</button>
            </center>
        </form>
    </div>
</div>
<script src="lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="lib/jq-module/zyupload/zyupload-1.0.0.min.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //年纪选择
        jq1111.ajax({
            type: "GET",
            crossDomain:true,
            url: BasePath+"api/School/Grades",
            contentType: "application/json",
            dataType:"json",
            xhrFields: {
                withCredentials: true
            },
            success:function (message) {
                console.log(message)
                var grades=message.data;
                grades.forEach(function (grade) {
                    jq1111("#grade-select-search-add").append('' +
                        '<option value="' +
                        grade.id +
                        '">' +
                        grade.display +
                        '</option>' +
                        '')
                })
                //再次初始化form
                form.render();
            },
            error:function (message) {
                console.log(message.responseJSON);
                layer.msg('失败:'+message.responseJSON.message);
            },
        });

        var ManageMajors
        //获取专业
        jq1111.ajax({
            type: "GET",
            crossDomain: true,
            async:false,
            url: BasePath + "api/Account",
            contentType: "application/json",
            dataType: "json",
            xhrFields: {
                withCredentials: true
            },
            success: function (message) {
                console.log(message)
                var majors = message.data.managedMajors;
                ManageMajors=majors;
                majors.forEach(function (major) {
                    jq1111("#major-select-search-add").append('' +
                        '<option value="' +
                        major.id +
                        '">' +
                        major.name +
                        '</option>' +
                        '')
                });
                //再次初始化form
                form.render('select');
            },
            error: function (message) {
                console.log(message.responseJSON);
            },
        });
        form.render('select');//select是固定写法 不是选择器

        //监听专业的select事件来设置年级
        form.on('select(major-select-search-add)', function(data){
            console.log(data)
            if(data.value === ""){
                jq1111('#grade-select-search-add').attr("disabled","disabled");
            }else{
                jq1111('#grade-select-search-add').removeAttr("disabled");
            }
            form.render('select');
        });

        //监听年级的select事件来设置年级
        form.on('select(grade-select-search-add)', function(data){
            console.log(data)
            if(data.value == ""){
                jq1111('#class-select-search-add').attr("disabled","disabled");
                form.render('select');
            }else{
                jq1111('#class-select-search-add').removeAttr("disabled");
                //移除之前添加的班级
                var children=jq1111("#class-select-search-add").children();
                for (i=1;i<children.length;i++){
                    children.eq(i).remove();
                }
                console.log('gradeid',jq1111('#grade-select-search-add'))
                //获取班级
                jq1111.ajax({
                    type: "GET",
                    crossDomain:true,
                    url: BasePath+'api/School/QueryClasses?majorId='+jq1111('#major-select-search-add').val()+'&gradeId='+jq1111('#grade-select-search-add').val(),
                    contentType: "application/json",
                    dataType:"json",
                    xhrFields: {
                        withCredentials: true
                    },
                    success:function (message) {
                        var classes=message.data;
                        classes.forEach(function (classt) {
                            jq1111("#class-select-search-add").append('' +
                                '<option value="' +
                                classt.id +
                                '">' +
                                classt.name +
                                '</option>' +
                                '')
                        });
                        //再次初始化form
                        form.render('select');
                    },
                    error:function (message) {
                        console.log(message.responseJSON);
                    },
                });
                form.render('select');//select是固定写法 不是选择器
            }

        });


        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        });

        //监听提交
        form.on('submit(save)', function (data) {
            var isPub=true;
            if(data.field.isPublic==='off'){
                isPub=false;
            }
            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                url: BasePath+"api/GroupAlbum/Add",
                contentType: "application/json",
                dataType:"json",
                xhrFields: {
                    withCredentials: true
                },
                data:JSON.stringify({
                    "title": data.field.title,
                    "description": data.field.describe,
                    "isPublic": isPub,
                    "classid":data.field.class
                }),
                success:function (message) {
                    console.log(message);
                    console.log(data);
                    layer.alert(message.msg, {
                        title: '操作结果'
                    })
                    location.reload()
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    layer.alert(message.responseJSON.msg, {
                        title: '操作结果'
                    })
                },
            });

            return false;
        });
    });
</script>